package com.nectech.entity;

import com.nectech.client.MapleStoryClient;
import com.nectech.constant.Constant;
import com.nectech.util.ImageUtil;

import java.awt.*;
import java.util.List;
import java.util.Random;

/**
 * @Author: 浅小北
 * @ClassName: Mob
 * @Description: TODO
 * @Date: 2021/1/28 19:23
 * @Since: version-1.0
 */
public class Mob extends AbstractMapleStoryObject{
    public static Image[] images = new Image[100];
    static {
        for (int i = 0; i < 3; i++) {
            images[i] = ImageUtil.getImage("mob_left_stand_" + i);
        }
        for (int i = 3; i < 6; i++) {
            images[i] = ImageUtil.getImage("mob_right_stand_" + (i - 3));
        }
        for (int i = 6; i < 10; i++) {
            images[i] = ImageUtil.getImage("mob_left_move_" + (i - 6));
        }
        for (int i = 10; i < 14; i++) {
            images[i] = ImageUtil.getImage("mob_right_move_" + (i - 10));
        }
        images[14] = ImageUtil.getImage("mob_left_hit");
        images[15] = ImageUtil.getImage("mob_right_hit");
        for (int i = 16; i < 20; i++) {
            images[i] = ImageUtil.getImage("mob_left_die_" + (i - 16));
        }
        for (int i = 20; i < 24; i++) {
            images[i] = ImageUtil.getImage("mob_right_die_" + (i - 20));
        }
        images[25] = ImageUtil.getImage("mob_blood");
        for (int i = 26; i < 35; i++) {
            images[i] = ImageUtil.getImage("mob_attack_" + (i - 26));
        }
    }
    public MapleStoryClient msc;
    public int MAX_HP;
    public Mob(){
        this.height = images[0].getHeight(null);
        this.width = images[0].getWidth(null);
        this.action = Action.WALK;
        this.speed = Constant.MOB_WALK_SPEED;
        this.live = true;
        this.MAX_HP = 10000;
        this.HP = MAX_HP;
    }
    public Mob(MapleStoryClient msc,int x,int y,Direction dir) {
        this();
        this.msc = msc;
        this.x = x;
        this.y = y;
        this.dir = dir;
    }
     /**
      * @Author: 浅小北
      * @Description: mob类重写draw方法
      * @Date: 19:30 2021/1/28
      * @Param:
      * @return:
      */
     private int count;
     private int step;
     private int dieStep;
    @Override
    public void draw(Graphics g) {
        if (dieStep >= 4) {
            this.live = false;
            dieStep = 0;
        }
        if (!live) {
            msc.mobList.remove(this);
            return;
        }
        switch (dir){
            case LEFT:
                switch (action) {
                    case STAND:
                        if (count++ % 3 == 0) {
                            g.drawImage(images[step++ % 3],x,y,null);
                        } else {
                            g.drawImage(images[step % 3],x,y,null);
                        }
                        break;
                    case WALK:
                        if (count++ % 3 == 0) {
                            g.drawImage(images[(step++ % 3) + 6],x,y,null);
                        } else {
                            g.drawImage(images[(step % 3) + 6],x,y,null);
                        }
                        break;
                    case HIT:
                            g.drawImage(images[14],x,y,null);
                        break;
                    case DIE:
                        if (count++ % 3 == 0) {
                            g.drawImage(images[(dieStep++ % 4) + 16],x,y,null);
                        } else {
                            g.drawImage(images[(dieStep % 4) + 16],x,y,null);
                        }
                        break;
                    case ATTACK:
                        if (count++ % 3 == 0) {
                            g.drawImage(images[(step++ % 9) + 26],x,y,null);
                        } else {
                            g.drawImage(images[(step % 9) + 26],x,y,null);
                        }
                        break;
                    default:
                        break;
                }
                break;
            case RIGHT:
                switch (action) {
                    case STAND:
                        if (count++ % 3 == 0) {
                            g.drawImage(images[step++ % 3 + 3],x,y,null);
                        } else {
                            g.drawImage(images[step % 3 + 3],x,y,null);
                        }
                        break;
                    case WALK:
                        if (count++ % 3 == 0) {
                            g.drawImage(images[(step++ % 3) + 10],x,y,null);
                        } else {
                            g.drawImage(images[(step % 3) + 10],x,y,null);
                        }
                        break;
                    case HIT:
                        g.drawImage(images[15],x,y,null);
                        break;
                    case DIE:
                        if (count++ % 3 == 0) {
                            g.drawImage(images[(dieStep++ % 4) + 20],x,y,null);
                        } else {
                            g.drawImage(images[(dieStep % 4) + 20],x,y,null);
                        }
                        break;
                    case ATTACK:
                        if (count++ % 3 == 0) {
                            g.drawImage(images[(step++ % 9) + 26],x,y,null);
                        } else {
                            g.drawImage(images[(step % 9) + 26],x,y,null);
                        }
                        break;
                    default:
                        break;
                }
                break;
            default:
                break;
        }
        move();
        if (HP < MAX_HP) {
            bloodBar.draw(g);
        }
    }

    /**
     * @Author: 浅小北
     * @Description: mob类重写move方法
     * @Date: 19:51 2021/1/28
     * @Param:
     * @return:
     */

    @Override
    public void move() {
        hit();
        mobStatus();
        mobOutOfBound();
        if (dir == Direction.LEFT) {
            if (action == Action.WALK) {
                x -= speed;
            }
        }
        if (dir == Direction.RIGHT) {
            if (action == Action.WALK) {
                x += speed;
            }
        }
    }
     /**
      * @Author: 浅小北
      * @Description: mob随机切换走和站的状态的方法
      * @Date: 10:59 2021/1/30
      * @Param:
      * @return:
      */
     private int mobChangeStatusCount = 0;
     private int mobStep = 1;
     private boolean mobWalkSwitch = true;
     public void mobStatus() {
         //TODO 算法、数值待优化
         if (mobWalkSwitch) {
             if (mobChangeStatusCount++ % 15 == 0) {
                 this.action = Action.WALK;
             }
             mobWalkSwitch = false;
         } else {
             if (mobStep++ % 50 == 0) {
                 this.action = Action.STAND;
             }
             mobWalkSwitch = true;
         }


     }

      /**
       * @Author: 浅小北
       * @Description: 随机mob运动方向
       * @Date: 12:37 2021/1/30
       * @Param:
       * @return:
       */
      public void dirConfirm() {

      }

       /**
        * @Author: 浅小北
        * @Description: mob出界问题，调转方向
        * @Date: 12:38 2021/1/30
        * @Param:
        * @return:
        */
       public void mobOutOfBound() {
           if (this.x <= 642
                   || this.x >= 1732 - this.width) {
               if (this.dir == Direction.LEFT) {
                   //System.out.println("出界变换右");
                   this.dir = Direction.RIGHT;
               } else if (this.dir == Direction.RIGHT) {
                   //System.out.println("出界变换左边的");
                   this.dir = Direction.LEFT;
               }
           }
       }
    
     /**
      * @Author: 浅小北
      * @Description: 使用内部类创建mob血条
      * @Date: 20:36 2021/1/28
      * @Param:
      * @return:
      */
     public BloodBar bloodBar = new BloodBar();
     class BloodBar extends AbstractMapleStoryObject {
         private Image img;
         public BloodBar(){
             //TODO
             this.img = ImageUtil.getImage("mob_blood");
             this.width = img.getWidth(null);
         }
         @Override
         public void draw(Graphics g) {
             /*Color c = g.getColor();
            //内部类访问外部类相同属性名的属性的时候，使用外部类.this.属性名
             g.drawRect(Mob.this.x,Mob.this.y - 15,Mob.this.width,10);
             //颜色填充
             g.setColor(Color.red);
             g.fillRect(Mob.this.x + 1,Mob.this.y - 15,Mob.this.width * Mob.this.HP / Mob.this.MAX_HP,9);
             g.setColor(c);*/
             int n = Mob.this.width / this.width * Mob.this.HP / Mob.this.MAX_HP;
             for (int i = 0; i < n; i++) {
                 g.drawImage(img,Mob.this.x + (i * this.width),Mob.this.y - 15,null);
             }

         }
     }

      /**
       * @Author: 浅小北
       * @Description: 怪物碰到人物掉血
       * @Date: 10:19 2021/2/2
       * @Param:
       * @return:
       */
      private int mob_hit_count = 0;
      public void hit() {
          mob_hit_count++;
        /**
         * 注意if里面的条件，让怪物和人物相交的判定时间延长
         */
        if (msc.hero.HP >= 0 && this.getRectangle().intersects(
                msc.hero.getRectangle())
                && mob_hit_count % 30 == 0) {
            this.action = Action.ATTACK;
            msc.hero.HP -= 30;
        }
    }

}


